package com.example.chinalin.viewpagerdemo;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.nineoldandroids.view.ViewHelper;

import ltd.viewpager.JazzyViewPager;
import ltd.viewpager.OutlineContainer;

public class ViewPagerScrollActivity extends AppCompatActivity {
    private JazzyViewPager mJazzy;
//    private RelativeLayout relativeLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_scroll);
//        relativeLayout = (RelativeLayout)super.findViewById(R.id.ltdrelativeLayout) ;
//        setupRelayout();
        setupJazziness(JazzyViewPager.TransitionEffect.Tablet);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Toggle Fade");
        String[] effects = this.getResources().getStringArray(R.array.jazzy_effects);
        for (String effect : effects)
            menu.add(effect);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().toString().equals("Toggle Fade")) {
            mJazzy.setFadeEnabled(!mJazzy.getFadeEnabled());
        } else {
            JazzyViewPager.TransitionEffect effect = JazzyViewPager.TransitionEffect.valueOf(item.getTitle().toString());
            setupJazziness(effect);
        }
        return true;
    }

    private void setupJazziness(JazzyViewPager.TransitionEffect effect) {
        mJazzy = (JazzyViewPager) findViewById(R.id.jazzy_pager);
        mJazzy.setTransitionEffect(effect);
        mJazzy.setAdapter(new MainAdapter());
        mJazzy.setPageMargin(30);
    }

    private class MainAdapter extends PagerAdapter {
        @Override
        public Object instantiateItem(ViewGroup container, final int position) {

            ScrollView scrollView = new ScrollView(ViewPagerScrollActivity.this);
            setupScrollView(scrollView);
            container.addView(scrollView, ViewGroup.MarginLayoutParams.MATCH_PARENT, ViewGroup.MarginLayoutParams.MATCH_PARENT);
            mJazzy.setObjectForPosition(scrollView, position);
            return scrollView;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object obj) {
            container.removeView(mJazzy.findViewFromObject(position));
        }
        @Override
        public int getCount() {
            return 10;
        }
        @Override
        public boolean isViewFromObject(View view, Object obj) {
            if (view instanceof OutlineContainer) {
                return ((OutlineContainer) view).getChildAt(0) == obj;
            } else {
                return view == obj;
            }
        }
    }


    private void setupScrollView(ScrollView scrollView){
//        scrollView.setScrollBarStyle(Scrollview.none);
        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(ViewPagerScrollActivity.this);
        RelativeLayout relativeLayout = new RelativeLayout(ViewPagerScrollActivity.this);
        setupRelayout(relativeLayout);
        horizontalScrollView.addView(relativeLayout);
        scrollView.addView(horizontalScrollView);
    }
//    private void setupRelayout(){
//        setupRelayout(ViewPagerScrollActivity.this.relativeLayout) ;
//    }
    private void setupRelayout(RelativeLayout relativeLayout){
        addButton(relativeLayout);
    }
    private  void addButton(RelativeLayout relativeLayout){
        for (int i=0; i<10 ; i++){
            for (int j=0;j<3 ;j++){
                Button button = new Button(this) ;

                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(300,300);
                lp.setMargins(i*300,j*300, 0, 0);
                button.setLayoutParams(lp);
                //            imageView.setLayoutParams(layoutParams);
                button.requestLayout();
                button.setText(i*10+j + "");
                relativeLayout.addView(button);
            }
        }
    }

    private  void addImageView(RelativeLayout relativeLayout){
        for (int i=0; i<10 ; i++){
            for (int j=0;j<10 ;j++){
                ImageView imageView = new ImageView(this) ;
                imageView.setFocusable(true);
                //                Glide
                //                        .with(ViewPagerScrollActivity.this)
                //                        .load(constant.eatFoodyImages[(i+j)%14])
                //                        .into(imageView);
                imageView.setImageResource(R.drawable.image_1);
                RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(300,300);
                lp.setMargins(i*300,j*300, 0, 0);
                imageView.setLayoutParams(lp);
                //            imageView.setLayoutParams(layoutParams);
                imageView.requestLayout();
                imageView.setId(i*10+j);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(ViewPagerScrollActivity.this,"id:"+v.getId(),Toast.LENGTH_SHORT).show();
                    }
                });
                imageView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus){
                            ImageView iv = (ImageView)v;
                            iv.setScaleType(ImageView.ScaleType.FIT_XY);
                            ViewHelper.setScaleX(v, 1.1f);
                            ViewHelper.setScaleY(v, 1.1f);
                        }else{
                            ImageView iv = (ImageView)v;
                            iv.setScaleType(ImageView.ScaleType.CENTER);
                            //                            ViewHelper.setScaleX(v, 1.0f);
                            //                            ViewHelper.setScaleY(v, 1.0f);
                        }
                    }
                });
                relativeLayout.addView(imageView);
            }
        }
    }

}
